import { reqCartList,reqCartCheck,reqDeletecart } from '@/api/request'

const state = {
    cartList:[],
}
const mutations = {
    GETCARTLIST(state,cartList){
        state.cartList = cartList
    }
}
const actions = {
    // 获取购物车列表的数据
    async getCartList({commit}){
        let result = await reqCartList()
        if(result.code===200){
            commit('GETCARTLIST',result.data)
        }
    },
    // 切换购物车列表选中状态 无返回值
    async cartchecked({commit},{skuId,isChecked}){
        let result = await reqCartCheck(skuId,isChecked)
        if(result.code===200){
            return '状态修改成功'
        }else{
            return Promise.reject(new Error('状态修改失败'))
        }
    },
    async deletecart({commit},{skuId}){
        let result = await reqDeletecart(skuId)
        if(result.code===200){
            return '商品删除成功'
        }else{
            return Promise.reject(new Error('商品删除失败'))
        }
    }
}
const getters = {
    cartList(state){
        return state.cartList[0] || {}
    }
}
export default{
    state,
    mutations,
    actions,
    getters,
}